package com.battery.web.controller.system.battery;

import java.util.Date;
import java.util.List;

import com.battery.common.enums.MemberAuditStatus;
import com.battery.common.enums.YesOrNoEnum;
import com.battery.common.utils.DateUtils;
import com.battery.system.domain.BO.MemberBO;
import com.battery.system.domain.VO.MemberVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.battery.common.annotation.Log;
import com.battery.common.enums.BusinessType;
import com.battery.system.domain.Member;
import com.battery.system.service.battery.IMemberService;
import com.battery.common.core.controller.BaseController;
import com.battery.common.core.domain.AjaxResult;
import com.battery.common.utils.poi.ExcelUtil;
import com.battery.common.core.page.TableDataInfo;

/**
 * VIP申请Controller
 *
 * @author battery
 * @date 2021-01-24
 */
@Controller
@RequestMapping("/system/member")
public class MemberController extends BaseController {
    private String prefix = "system/member";

    @Autowired
    private IMemberService memberService;

    @RequiresPermissions("system:member:view")
    @GetMapping()
    public String member() {
        return prefix + "/member";
    }

    /**
     * 查询VIP申请列表
     */
    @RequiresPermissions("system:member:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MemberBO memberBO) {
        startPage();
//        List<Member> list = memberService.selectMemberList(member);
        List<MemberVO> list = memberService.selectMemberVOList(memberBO);
        return getDataTable(list);
    }

    /**
     * 导出VIP申请列表
     */
    @RequiresPermissions("system:member:export")
    @Log(title = "VIP申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Member member) {
        List<Member> list = memberService.selectMemberList(member);
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member.class);
        return util.exportExcel(list, "member");
    }

    /**
     * 新增VIP申请
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存VIP申请
     */
    @RequiresPermissions("system:member:add")
    @Log(title = "VIP申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Member member) {
        return toAjax(memberService.insertMember(member));
    }

    /**
     * 修改VIP申请
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Member member = memberService.selectMemberById(id);
        mmap.put("member", member);
        return prefix + "/edit";
    }

    /**
     * 修改保存VIP申请
     */
    @RequiresPermissions("system:member:edit")
    @Log(title = "VIP申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Member member) {
        return toAjax(memberService.updateMember(member));
    }

    /**
     * 删除VIP申请
     */
    @RequiresPermissions("system:member:remove")
    @Log(title = "VIP申请", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(memberService.deleteMemberByIds(ids));
    }


    /**
     * 升级为超级VIP
     */
    @GetMapping("/promotionSuperVip/{id}")
    public String promotionSuperVip(@PathVariable("id") String id, ModelMap mmap) {
        Member member = memberService.selectMemberById(id);
        mmap.put("member", member);
        return prefix + "/promotionSuperVip";
    }

    /**
     * 修改保存VIP申请
     */
    @RequiresPermissions("system:member:promotionSuperVip")
    @Log(title = "VIP申请", businessType = BusinessType.UPDATE)
    @PostMapping("/promotionSuperVip")
    @ResponseBody
    public AjaxResult promotionSuperVip(Member member) {

        if (StringUtils.isBlank(member.getId())) {
            return error("主键ID为空");
        }
        if (member.getOrderFreeMinute() <= 0) {
            return error("请填写‘每单前X分钟免费’，且不能小于等于0");
        }
        if (member.getDailyOrderLimit() <= 0) {
            return error("请填写‘每天免费订单数’，且不能小于等于0");
        }
        Date date = DateUtils.getNowDate();
        member.setUpdateTime(date);
        member.setIsSuper(YesOrNoEnum.YES.getCode());

        // 升级成超级VIP,直接将审核状态变更位已审核
        member.setAuditStatus(MemberAuditStatus.ALREADY_PASSED.getCode());
        member.setApprovalTime(date);
        return toAjax(memberService.updateMember(member));
    }


    /**
     * 解除VIP
     *
     * @param id
     * @return
     */
    @RequiresPermissions("system:member:relieve")
    @Log(title = "VIP申请", businessType = BusinessType.DELETE)
    @PostMapping("/relieve")
    @ResponseBody
    public AjaxResult relieve(String id) {

        Member memberUpdate = new Member();
        memberUpdate.setId(id);
        memberUpdate.setIsDelete(YesOrNoEnum.YES.getCode());

        return toAjax(memberService.updateByPrimaryKeySelective(memberUpdate));
    }


}
